package com.gujianjian.testjwt.controller;

import com.gujianjian.testjwt.exceptions.LoginFailed;
import com.gujianjian.testjwt.exceptions.NeedToLogin;
import com.gujianjian.testjwt.exceptions.TokenUnavailable;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 顾小健
 * @create: 2021-12-12
 */
@RestControllerAdvice
public class GlobalController {

    @ExceptionHandler(NeedToLogin.class)
    public Map<String,Object> needToLogin(){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 403);
        map.put("msg", "你需要登录");
        return  map;
    }
    @ExceptionHandler(TokenUnavailable.class)
    public Map<String,Object> tokenUnavailable(){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 403);
        map.put("msg", "登录已过期，请重新登录");
        return  map;
    }

    @ExceptionHandler(LoginFailed.class)
    public Map<String,Object> loginFailed(){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 403);
        map.put("msg", "登录失败！");
        return  map;
    }
}
